<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\RoomsResource;
use Illuminate\Support\Facades\Validator;
use App\Models\Room;
use Illuminate\Http\Request;

class RoomsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request,Room $room)
    {
        $validator = Validator::make($request->all(), [
            'shop_id' => 'required|numeric',
        ],[
            'required'=>':attribute是必须的',
            'numeric'=>':attribute必须是数字',
        ]);
        if($validator->fails()){
            foreach ($validator->errors()->messages() as $message){
                return $this->error($message[0]);
            }
        }
        $query=$room->newQuery();
        $list=$query->where('sid',$request->shop_id)->where('status',1)
            ->paginate(10);
        if($list->isEmpty()){
            return $this->error('数据查询为空');
        }
        return RoomsResource::collection($list);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request,Room $room)
    {
        $validator = Validator::make($request->all(), [
            'room_id' => 'required|numeric',
        ],[
            'required'=>':attribute是必须的',
            'numeric'=>':attribute必须是数字',
        ]);
        if($validator->fails()){
            foreach ($validator->errors()->messages() as $message){
                return $this->error($message[0]);
            }
        }
        $query=$room->newQuery();
        $list=$query->where('id',$request->room_id)->where('status',1)->get();
        if($list->isEmpty()){
         return $this->error('数据查询为空');
        }
        return RoomsResource::collection($list)->resource;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
